package com.anlu.base.jdk.jdk8;

import com.anlu.base.jdk.jdk8.functions.MyFun;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class FunctionInterfaceDemo1 {

    public static void main(String[] args) {


//        test1();

        test3();
    }

    static void test(){
        //使用函数式接口
        String result1 = toLowerCase(new MyFun<String>() {
            @Override
            public String getValue(String s) {
                return s.toUpperCase();
            }
        },"hello");



        String result2 = toLowerCase( s -> s.toUpperCase(), "hello");
        System.out.println("result2:" + result2);
    }

    // 定义一个方法，将给定的字符串转为大写
    public static String toUpperCase(String str) {
        return str.toUpperCase();
    }

    /**
     * 将给定的字符串转为大写
     * @param function 函数式接口
     * @param str 给定字符串
     * @return
     */
    public static String toLowerCase(MyFun<String> function, String str) {
        return function.getValue(str);
    }

    static void test1(){
        getMax(new Supplier<Integer>() {
            @Override
            public Integer get() {
                //1.定义一个数组
                int[] arr = {12,23,34,94,555,60,27,38,99};
                //2.对数组进行排序
                Arrays.sort(arr);
                //3.将最大值返回
                return arr[arr.length - 1];
            }
        });

        getMax(() -> {
            //1.定义一个数组
            int[] arr = {12,23,34,94,555,60,27,38,99};
            //2.对数组进行排序
            Arrays.sort(arr);
            //3.将最大值返回
            return arr[arr.length - 1];
        });
    }

    static void getMax(Supplier<Integer>  supplier){
        int max = supplier.get();
        System.out.println(max);
    }


    static void test2(){
        transform(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s.toUpperCase());
            }
        });

        transform(s -> System.out.println(s.toLowerCase()));
    }
    static void transform(Consumer<String> consumer){
        consumer.accept("hello consumer");
    }

    public static int calculate(Function<String,Integer> function) {
        Integer value = function.apply("10");
        return value + 5;
    }
    public static int calculate1(Function<String,Integer> f1, Function<Integer,Integer> f2) {
        Integer value2 = f1.andThen(f2).apply("10");
        return value2;
    }

    static void test3(){
        System.out.println(calculate(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        }));

        System.out.println(calculate(s->Integer.parseInt(s)));

        System.out.println(calculate1(s->Integer.parseInt(s),i->i+5));
    }


    static void test4(){
        BiFunction<Integer,Integer,Double> biFunction = (a,b)->Math.pow(a,b);
        Function<Double,String> function = a->"result:" + String.valueOf(a);
        System.out.println(biFunction.andThen(function).apply(3,4));
    }


}
